package com.vipheyue.bigcompany.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;

/**
 * Created by heyue on 2015/10/31.
 */
public class HttpUtil {
    public final static int GET = 1;
    public final static int POST = 2;
    public final static int SUCCESS = 3;
    public final static int FAILURE = 4;
    public final static int COMMITSUCCESS = 5;
    public final static int OTHERSUCCESS = 11;
    HttpRequest.HttpMethod httpMethod = null;
    Context context = null;

    HttpUtil(Context context) {
        this.context = context;
    }

    public void setMethod(HttpRequest.HttpMethod httpMethod) {
        this.httpMethod = httpMethod;
    }

    public <T> void getData(final String url, final Handler handler, RequestParams requestParams, final Class<T> cls, final int msgType) {
//        final T t = null;
        HttpUtils http = new HttpUtils();
        if (httpMethod == null) {//设置默认请求方式
            synchronized (HttpUtil.class) { //手动发请求其实也不用加锁
                if (httpMethod == null) {
                    httpMethod = HttpRequest.HttpMethod.POST;
                }
            }
        }

        if (requestParams == null) {
            requestParams = new RequestParams();
        }
        http.send(httpMethod, url, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                T t = getObject(responseInfo.result, cls);
                LogUtils.i("responseInfo.result:" + responseInfo.result);
                Message msg = Message.obtain();
                msg.what = msgType;
                msg.obj = t;
                if (t != null) {
                    handler.sendMessage(msg);
                } else {
                    LogUtils.i("HttpUtil:解析对象为空,");
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Message msg = Message.obtain();
                msg.what = FAILURE;
                msg.obj = s;
                handler.sendMessage(msg);
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                super.onLoading(total, current, isUploading);
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
    }

    public static <T> T getObject(String jsonString, Class<T> clazz) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, clazz);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.i("HttpUtil:无法解析json到class,请检查json格式");
            LogUtils.i("jsonString格式---->:  " + jsonString);
        }
        return t;
    }
}
